cmake_minimum_required(VERSION 3.8)
project(navigation)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic -std=c++17)
endif()

if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(rviz_common REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(livox_ros_driver2 REQUIRED)
find_package(PCL REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(manipulation REQUIRED)

add_definitions(-DMAP_MANAGER_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../map_manager\") 

######################## CPP ########################
# QT5依赖
set(CMAKE_AUTOMOC ON)
qt5_wrap_cpp(MOC_FILES  # 关键: 启动 Qt5 自动 MOC 处理 (否则rviz2读取插件报错)
  include/navigation/rviz2_gui/rviz2_gui_panel.hpp
)

# 可执行文件和库
add_executable(demo_twist src/demo_twist.cpp)
add_executable(nav_core src/nav_core.cpp)
add_library(rviz2_gui_panel SHARED src/rviz2_gui_panel.cpp ${MOC_FILES})

# 头文件路径
target_include_directories(demo_twist PUBLIC
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_include_directories(nav_core PUBLIC
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp>
  ${PCL_INCLUDE_DIRS}
)
target_include_directories(rviz2_gui_panel PUBLIC
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp>
)

# 连接库和依赖
target_link_libraries(nav_core navigation__rosidl_typesupport_cpp ${PCL_LIBRARIES})
target_link_libraries(rviz2_gui_panel Qt5::Widgets navigation__rosidl_typesupport_cpp)
ament_target_dependencies(demo_twist
  rclcpp geometry_msgs sensor_msgs)
ament_target_dependencies(nav_core
  rclcpp geometry_msgs sensor_msgs nav_msgs tf2 tf2_ros tf2_geometry_msgs visualization_msgs std_msgs livox_ros_driver2 pcl_conversions manipulation)
ament_target_dependencies(rviz2_gui_panel
  rclcpp geometry_msgs sensor_msgs nav_msgs tf2 tf2_ros tf2_geometry_msgs visualization_msgs rviz_common pluginlib std_msgs std_srvs)

# 安装规则
install(TARGETS demo_twist nav_core rviz2_gui_panel
  DESTINATION lib/${PROJECT_NAME}
)
install(DIRECTORY include DESTINATION include)
install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})
install(DIRECTORY config DESTINATION share/${PROJECT_NAME})
install(FILES rviz2_gui_panel_plugins.xml DESTINATION share/${PROJECT_NAME})

# 生成插件描述文件
ament_export_include_directories(include)
pluginlib_export_plugin_description_file(rviz_common rviz2_gui_panel_plugins.xml)

# 生成ROS消息和服务接口
rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/TargetsList.msg"
  "srv/Rviz2Nav.srv"
  DEPENDENCIES geometry_msgs nav_msgs
)

######################## Python ########################
install(PROGRAMS scripts/map_adjuster.py DESTINATION lib/${PROJECT_NAME})

# 生成ROS包环境变量
ament_package()
